<?php
namespace backend\controllers;

use Yii;

use yii\data\Pagination; //分页
use backend\base\BaseController;
use backend\models\AreaModel;
use backend\models\SignModel;

/**
 * Sign controller
 */
class SignController extends BaseController
{
    /*
     * 区域签约
     */
    public function actionIndex()
    {
        $data = parent::parseData($this->body);
        $model = $city = [];
        if(!empty($data['city']) || !empty($data['title']) || !empty($data['sign'])){
            $model = new SignModel();
            $model = $model->list($data);
            if(!empty($model)){
                foreach ($model as $key => $value) {
                    $model[$key]['message'] = self::class(!empty($value['areaLevel']) ? $value['areaLevel'] : '');
                }
            }
        }
        if(!empty($data['province'])){
            $city = (new AreaModel())->getArea($data['province']);
        }
        $province = (new AreaModel())->getArea();
        return $this->render('index',array(
            'data'=>$data,
            'province'=>$province,
            'city'=>$city,
            'model'=>$model
        ));
    }

    /*
     * 获取城市
     */
    public function actionGetList()
    {
        $data = parent::parseData($this->body);
        if(!empty($data)){
            $model = new AreaModel();
            $list = $model->getArea($data['province']);
            if(!empty($list)){
                return json_encode(parent::ajaxCustom('20000', 'success', $list));
            }
        }
        return json_encode(parent::ajaxCustom('80000', 'param_error', []));//返回参数验证失败信息
    }

    /*
     * 区域分类
     */
    public function class($id = '')
    {
        $result = [
            '1'=>'一线城市主城区(25万)',
            '2'=>'一线城市非主城区(20万)',
            '3'=>'二线城市主城区(20万)',
            '4'=>'二线城市非主城区(15万)',
            '5'=>'暂无报价基础(15万)',
        ];
        if(!empty($id)){
            return $result[$id];
        }
        return $result;    
    }

    /*
     * 签约确认
     */
    public function actionSignAjaxDo()
    {
        $data = parent::parseData($this->body);
        if(!empty($data['id'])){
            $sign = new SignModel();
            if ($sign->load($data, '') && $sign->validate()) {
                if($data['isSign'] == 0){
                    $count = $sign->updateAll(['isSign'=>1],'id=:id',array(':id'=>$data['id']));
                }
                if($data['isSign'] == 1){
                    $count = $sign->updateAll(['isSign'=>0],'id=:id',array(':id'=>$data['id']));
                }
                if($count> 0){
                    return json_encode(parent::ajaxReturn('operate_success_edit', []));
                }
            }
            $errorMsg = array_values($video->getErrors());//报错信息
            return json_encode(parent::ajaxCustom('80000', $errorMsg));
        }
        return json_encode(parent::ajaxCustom('80000', 'param_error', []));//返回参数验证失败信息         
    }

    /*
     * 城市区域设置
     */
    public function actionCitys()
    {
        $data = parent::parseData($this->body);
        $model = $city = [];
        if(!empty($data['city'])){
            $model = new SignModel();
            $model = $model->list($data);
            if(!empty($model)){
                foreach ($model as $key => $value) {
                    $model[$key]['message'] = self::class(!empty($value['areaLevel']) ? $value['areaLevel'] : '');
                }
            }
            $city = (new AreaModel())->getArea($data['province']);
        }
        $province = (new AreaModel())->getArea();
        $level = self::class();
        return $this->render('citys',array(
            'data'=>$data,
            'province'=>$province,
            'city'=>$city,
            'level'=>$level,
            'model'=>$model
        ));
    }

    /*
     * 设置城市级别
     */
    public function actionCityLevelAjaxDo()
    {
        $data = parent::parseData($this->body);
        if(!empty($data['id'])){
            $sign = new SignModel();
            if ($sign->load($data, '') && $sign->validate()) {
                $count = $sign->updateAll(['areaLevel'=>$data['level']],'id=:id',array(':id'=>$data['id']));
                if($count> 0){
                    return json_encode(parent::ajaxReturn('operate_success_edit', []));
                }
            }
            $errorMsg = array_values($video->getErrors());//报错信息
            return json_encode(parent::ajaxCustom('80000', $errorMsg));
        }
        return json_encode(parent::ajaxCustom('80000', 'param_error', []));//返回参数验证失败信息         
    }

}
